<?php
/**
 * 用户上传素材
 */
namespace Home\Controller;
use Think\Controller;
class MaterialController extends InitController {
	
	//上传素材列表
	public function upload_material_list(){
		
		$list = D('UploadMaterial')->upload_material_list($this->uid);
		
		$this->assign(array(
			'type_list'=>array(
				'1'=>'文章',
				'2'=>'图片',
				'3'=>'视频',
			),
			'pass_list'=>array(
				'-1'=>'不通过',
				'0'=>'审核',
				'1'=>'通过',
			),
			'list'=>$list
		));
		$this->display('upload_material_list');
	}
		
	
	//异步加载素材
	public function ajax_upload_material_list(){
		
		if(IS_AJAX){
			//异步
			$page         = I('page',0,'intval');
			$limit        = '';
			$index        = 10;
			$page_all     = 0;

			//获取页数
			if(!empty($page)){
				$page_all = $page * $index;
				$limit    = $page_all.','.$index;
			}else{
				$limit    = '0,10';
			}
			$list = D('UploadMaterial')->upload_material_list($this->uid,$limit);
			if(!empty($list)){
				$status = 1;
			}else{
				$status = -1;
			}
			
			return $this->ajaxReturn(array('data'=>$list,'status'=>$status));die;
		}
	}
	
	
	//用户上传素材
	public function upload_material(){
		$mid  = I('mid');
		$rc = D('UploadMaterial')->upload_material_find($mid);
		
		$this->assign(array(
			'ad'=>$rc,
		));
		$this->display('upload_material');
	}
	
	//用户删除素材
	public function upload_material_delete(){
		if(IS_AJAX){
			$mid = I('mid');
			if(empty($mid)){
				$this->error('非法操作');
			}
			
			$rc = D('UploadMaterial')->upload_material_delete($mid);
		
			if($rc){
				$this->success('返回成功');
			}else{
				$this->error('返回失败');
			}
			
		}
	}
	
	public function ajax_upload_material(){
		if(IS_AJAX){
			$type  = I('type');
			$title = I('title');
			$upload_address = I('upload_address'); //图片上传地址
			$upload_link = I('upload_link');       //链接
			$id    = I('id');
			$video_picture = '';
			//判断标题
			if(!$title){
				$this->error('请输入标题');
			}
			
			if($type == '1'){
				if(!$upload_link){
					$this->error('请输入链接');
				}
				
				//重置图片上传地址
				$upload_address = '';
				
			}else if($type == '3'){
				
				if(!$upload_link){
					$this->error('请输入链接');
				}
				
				if(!$upload_address){
					$this->error('请上传图片');
				}
				
				//去除图片左边的/
				$upload_address = ltrim($upload_address,'/');
				
				//视频封面图片
				$video_picture = $upload_address;
				
			}else if($type == '2'){
				
				if(!$upload_address){
					$this->error('请上传图片');
				}else{
					//去除图片左边的/
					$upload_address = ltrim($upload_address,'/');
					
					//链接
					$upload_link    = '';
					
				}
				
			}else{
				$this->error('非法参数');
			}
			
			//判断id
			if($id){
				$data = array(
					'title'=>$title,
					'upload_address'=>$upload_address,
					'type'=>$type,
					'createtime'=>NOW_TIME,
					'uid'=>$this->uid,
					'upload_link'=>$upload_link,
					'video_picture'=>$video_picture,
				);
				$rc = D('UploadMaterial')->upload_material_save($id,$data);
				
				if($rc){
					$this->success('返回成功');
				}else{
					$this->error('返回失败');
				}
				
			}else{
				$data = array(
					'title'=>$title,
					'upload_address'=>$upload_address,
					'type'=>$type,
					'createtime'=>NOW_TIME,
					'uid'=>$this->uid,
					'upload_link'=>$upload_link,
					'video_picture'=>$video_picture,
				);
				
				$rc = D('UploadMaterial')->upload_material_add($data);
				if($rc){
					$this->success('返回成功');
				}else{
					$this->error('返回失败');
				}
			}
		}
	}
}
